﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditVisa : AdminPageBase
    {
        private VisaInfo _visaInfo = new VisaInfo();
        private readonly VisaController _visaController = new VisaController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["VisaId"]))
                _visaInfo = _visaController.GetVisa(int.Parse(Request.QueryString["VisaId"]));

            if (Page.IsPostBack) return;

            LoadPartner();
            LoadStatus();
            LoadUser();
            LoadNational();

            if (_visaInfo.VisaId == -1) return;

            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListVisa.aspx?xml=Visa");
        }

        private void SaveData()
        {
            _visaInfo.VisaName = txtVisaName.Text;
            _visaInfo.PartnerId = int.Parse(ddlPartner.SelectedValue);
            _visaInfo.Status = int.Parse(ddlStatus.SelectedValue);
            _visaInfo.SortDescription = txtSortDesription.Text;
            _visaInfo.NationalId = int.Parse(ddlNational.SelectedValue);
            

            if (_visaInfo.VisaId == -1)
            {
                _visaInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _visaInfo.CreatedDate = DateTime.Now;
                _visaController.InsertVisa(_visaInfo);
            }
            else
            {
                _visaInfo.UpdatedBy = UserId;
                _visaInfo.UpdatedDate = DateTime.Now;
                _visaController.UpdateVisa(_visaInfo);
            }

            Response.Redirect("/Pages/ListVisa.aspx?xml=Visa");
        }

        private void RenderForm()
        {
            txtVisaName.Text = _visaInfo.VisaName;
            txtSortDesription.Text = _visaInfo.SortDescription;
            txtContent.Text = _visaInfo.DetailDescription;

            ddlStatus.SelectedValue = _visaInfo.Status.ToString();
            ddlCreatedBy.SelectedValue = _visaInfo.CreatedBy.ToString();
            ddlPartner.SelectedValue = _visaInfo.PartnerId.ToString();
            ddlNational.SelectedValue = _visaInfo.NationalId.ToString();
        }

        private void LoadPartner()
        {
            ddlPartner.DataSource =
               new PartnerController().ListPartner()
                   .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                   .OrderBy(p => p.PartnerName);
            ddlPartner.DataTextField = "PartnerName";
            ddlPartner.DataValueField = "PartnerId";
            ddlPartner.DataBind();

            ddlPartner.Items.Insert(0, new ListItem("--- Chọn đối tác ---", "-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadNational()
        {
            ddlNational.DataSource = new NationalController().ListNational();
            ddlNational.DataTextField = "NationalName";
            ddlNational.DataValueField = "NationalId";
            ddlNational.DataBind();

            ddlNational.Items.Insert(0, new ListItem("--- Chọn lãnh sự ---", "-1"));
        }
    }
}